<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item >营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>广告管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="AdvertiseFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="AdvertiseFormData.advertiseName" placeholder="请输入名字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="initAdvertiseTable">查询</el-button>
                    <el-button type="primary" @click="addAdvertise('addForm')">添加</el-button>
                </el-form-item>
            </el-form>
            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="advertiseName"
                            label="名字"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="positionName"
                            label="广告位置名称"
                            width="60">
                    </el-table-column>
                    <el-table-column
                            prop="pic"
                            label="图片"
                            width="80">
                        <template   slot-scope="scope">
                            <img :src="scope.row.pic"  min-width="70" height="80" />
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间"
                            width="160">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间"
                            width="160">
                    </el-table-column>
                    <el-table-column
                            prop="status"
                            label="状态"
                            width="80">
                        <template slot-scope="scope">
                            <el-switch
                                    @change="changeStatus(scope.row.id,scope.row.status)"
                                    v-model="scope.row.status"
                                    active-color="#13ce66"
                                    :active-value="0"
                                    :inactive-value="1"
                                    inactive-color="#ff4949">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="clickCount"
                            label="点击数"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="orderCount"
                            label="下单数"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="连接地址">
                    </el-table-column>
                    <el-table-column
                            prop="note"
                            label="备注">
                    </el-table-column>
                    <el-table-column
                            prop="sort"
                            label="排序"
                            width="80">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="changeTable(scope.row.id)">编辑</el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)">删除</el-button>
                            <!--<el-button type="warning" icon="el-icon-menu" circle @click="showRoleMenu(scope.row.roleId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
            <!--添加-->
            <el-dialog
                    title="提示"
                    :visible.sync="addDialogVisible"
                    width="30%"
            >
                <!--model:表单数据-->
                <el-form  label-width="80px" :model="addForm" :rules="rules" ref="addForm">
                    <el-form-item label="名字" prop="advertiseName">
                        <el-input v-model="addForm.advertiseName"></el-input>
                    </el-form-item>
                    <el-form-item label="位置" prop="positionId">
                        <el-select v-model="addForm.positionId" placeholder="请选择位置">
                            <el-option v-for="item in xiala" :key="item.positionName"
                                       :label="item.positionName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="图片" prop="pic">
                        <el-upload
                                class="avatar-uploader"
                                action="http://localhost:80/market/upload"
                                :show-file-list="false"
                                :headers="{'token' :token}"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>

                    <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker
                                v-model="addForm.startTime"
                                type="datetime"
                                placeholder="选择日期时间"
                                format="yyyy 年 MM 月 dd 日 HH 分 mm 秒"
                                value-format="yyyy-MM-dd-HH-mm-ss"
                                :picker-options="pickerOptions"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker
                                v-model="addForm.endTime"
                                type="datetime"
                                placeholder="选择日期时间"
                                format="yyyy 年 MM 月 dd 日 HH 分 mm 秒"
                                value-format="yyyy-MM-dd-HH-mm-ss"
                                :picker-options="pickerOptions"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="上下线状态" prop="status">
                        <el-switch
                                v-model="addForm.status"
                                active-color="#13ce66"
                                :active-value="0"
                                :inactive-value="1"
                                inactive-color="#ff4949">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="点击数" prop="clickCount">
                        <el-input v-model="addForm.clickCount"></el-input>
                    </el-form-item>
                    <el-form-item label="下单数" prop="orderCount">
                        <el-input v-model="addForm.orderCount"></el-input>
                    </el-form-item>
                    <el-form-item label="连接地址" prop="url">
                        <el-input v-model="addForm.url"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="note">
                        <el-input v-model="addForm.note"></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="addForm.sort"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="addDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="addbyAdvertiseId(`addForm`)">确 定</el-button>
                    </span>
            </el-dialog>
            <!--修改-->
            <el-dialog
                    title="提示"
                    :visible.sync="changeDialogVisible"
                    width="30%"
            >
                <!--model:表单数据-->
                <el-form  label-width="80px" :model="changeForm" :rules="rules" ref="changeForm">
                    <el-form-item label="名字" prop="advertiseName">
                        <el-input v-model="changeForm.advertiseName"></el-input>
                    </el-form-item>
                    <el-form-item label="位置" prop="positionId">
                        <el-select v-model="changeForm.positionId" placeholder="请选择位置">
                            <el-option v-for="item in xiala" :key="item.positionName"
                                       :label="item.positionName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="图片" prop="pic">
                        <el-upload
                                class="avatar"
                                action="http://localhost:80/market/upload"
                                :show-file-list="false"
                                :headers="{'token' :token}"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="开始时间" prop="startTime">
                        <el-date-picker
                                v-model="changeForm.startTime"
                                type="datetime"
                                placeholder="选择日期时间"
                                format="yyyy 年 MM 月 dd 日 HH 分 mm 秒"
                                value-format="yyyy-MM-dd-HH-mm-ss"
                                :picker-options="pickerOptions"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="结束时间" prop="endTime">
                        <el-date-picker
                                v-model="changeForm.endTime"
                                type="datetime"
                                placeholder="选择日期时间"
                                format="yyyy 年 MM 月 dd 日 HH 分 mm 秒"
                                value-format="yyyy-MM-dd-HH-mm-ss"
                                :picker-options="pickerOptions"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="点击数" prop="clickCount">
                        <el-input v-model="changeForm.clickCount"></el-input>
                    </el-form-item>
                    <el-form-item label="下单数" prop="orderCount">
                        <el-input v-model="changeForm.orderCount"></el-input>
                    </el-form-item>
                    <el-form-item label="连接地址" prop="url">
                        <el-input v-model="changeForm.url"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="note">
                        <el-input v-model="changeForm.note"></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="changeForm.sort"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                        <el-button @click="changeDialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="changebyAdvertiseId(`changeForm`)">确 定</el-button>
                    </span>
            </el-dialog>
            <!--删除确认弹窗-->
            <el-dialog
                    title="提示"
                    :visible.sync="dialogVisible"
                    width="30%"
            >
                <span>是否确认删除</span>
                <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="delQuestion()">确 定</el-button>
                </span>
            </el-dialog>


        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Advertise",
        data(){
            return {
                AdvertiseFormData:{},
                tableData:[],
                currentPage:1,
                pageSize:5,
                total:0,
                addForm:{},
                addDialogVisible:false,
                pickerOptions:{},//时间
                //声明当前的角色id号
                delid: 0,
                //删除确认弹窗
                dialogVisible:false,
                //修改弹窗
                changeDialogVisible:false,
                changeForm:[],
                //图片上传
                imageUrl: '',
                token:"",
                xiala:[],
                rules:{
                    advertiseName: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                        { min: 2, max: 9, message: '长度在 2 到 9 个字符', trigger: 'blur' }
                    ],
                    positionId: [
                        { required: true, message: '请输入位置', trigger: 'blur' },
                    ],
                    startTime: [
                        { required: true, message: '请输入开始时间', trigger: 'blur' }
                    ],
                    endTime: [
                        { required: true, message: '请输入结束时间', trigger: 'blur' }
                    ],
                    clickCount: [
                        { required: true, message: '请输入点击数', trigger: 'blur' },
                        {pattern:/^[1-9]\d*$/,message:'请输入正整数',trigger:'blur'}
                    ],
                    orderCount: [
                        { required: true, message: '请输入下单数', trigger: 'blur' },
                        {pattern:/^[1-9]\d*$/,message:'请输入正整数',trigger:'blur'}
                    ],
                    sort: [
                        { required: true, message: '请排序', trigger: 'blur' },
                        {pattern:/^[0-9]\d*$/,message:'请输入非负整数',trigger:'blur'},
                        {pattern:/^\d{1}$/,message:'请输入个位数',trigger:'blur'},
                    ],
                    url:[
                        {pattern:/^(https?:)/,message:'开头输入‘https:’',required:true,trigger:'blur'}
                    ],
                    note: [
                        { required: true, message: '请输入备注', trigger: 'blur' }
                    ],
                }
            }
        },
        created() {
            this.initAdvertiseTable();

        },
        methods:{
            initAdvertiseTable(){
                var that= this;
                this.$http.get("/market/position/queryPosition").then( function(resp) {
                    that.xiala=resp.data.result.positions

                })
                this.$http.post(`/market/advertise/initAdvertiseTable/${this.currentPage}/${this.pageSize}`,this.AdvertiseFormData).then(function (r) {
                    that.tableData=r.data.result.records;
                    that.total=r.data.result.total;
                    console.log(that.tableData)

                })



            },
            //新增
            addAdvertise(){
                var that=this;
                that.addDialogVisible=true;
                that.addForm={};
                this.imageUrl='';
                that.token=sessionStorage.getItem("token");

            },
            //确认新增
            addbyAdvertiseId(formName){
                this.addForm.pic=this.imageUrl;
                var that=this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        that.addDialogVisible=false;
                        this.$http.post(`/market/advertise/addbyAdvertiseId`,this.addForm).then( function(resp) {

                            that.$message.success(resp.data.result);
                            that.initAdvertiseTable();
                        })
                    }else {
                        return false;
                    }
                });
            },

            //编辑
            changeTable(id){
                var that=this;
                that.changeDialogVisible=true;
                that.token=sessionStorage.getItem("token");
                this.$http.get("/market/advertise/chaXun?id="+id).then(function (resp) {
                    that.changeForm=resp.data.result.advertises[0];
                    that.imageUrl=resp.data.result.advertises[0].pic;

                })
            },
            //确认编辑
            changebyAdvertiseId(formName){
                var that=this;
                that.changeForm.pic=that.imageUrl;
                this.$refs[formName].validate((valid) => {
                   if (valid) {
                       this.$http.post(`/market/advertise/changeAdvertise`,this.changeForm).then( function(resp) {
                           that.changeDialogVisible=false;
                           that.initAdvertiseTable();
                           that.$message.success(resp.data.result);
                       })
                   } else {
                       return false;
                   }
                });

            },
            //删除
            del(id){
                var that=this;
                that.delid=id;
                that.dialogVisible=true;
            },
            //确认删除
            delQuestion(){
                var that=this;
                this.$http.delete("/market/advertise/delAdvertise?id="+this.delid).then(function (resp) {
                    that.dialogVisible=false;
                    that.initAdvertiseTable();
                    that.$message({
                        message: '删除成功！！！',
                        type: 'success'
                    });
                })
            },

            //改变状态
            changeStatus(id,status){
                var that=this;
                this.$http.put("/market/advertise/changeStatus?id="+id,"&status="+status).then(function (resp) {

                })
            },

            //图片上传
            handleAvatarSuccess(res, file) {
                this.imageUrl =res.result.path;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },

            //当改变没有显示的条数时触发该方法
            handleSizeChange(y){
                this.pageSize=y;
                this.initAdvertiseTable();
            },
            //切换页码时触发该方法
            handleCurrentChange(y){
                this.currentPage=y;
                this.initAdvertiseTable();
            }

        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>